// <!--
//  * 严肃声明：
//  * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
//  * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
//  * Copyright (c) 2022 APThink David唐 all rights reserved.
//  * 版权所有，侵权必究！
//  *
// -->
//数据对象克隆
import log from 'loglevel'

export function cloneObject(obj: any): any {
    // Handle the 3 simple types, and null or undefined
    if (null == obj || "object" != typeof obj) return obj
    // Handle Date
    if (obj instanceof Date) {
        const copy = new Date()
        copy.setTime(obj.getTime())
        return copy
    }
    // Handle Array
    if (obj instanceof Array) {
        const copy = []
        for (let i = 0, len = obj.length; i < len; ++i) {
            copy[i] = cloneObject(obj[i])
        }
        return copy
    }
    // Handle Object
    if (obj instanceof Object) {
        const copy: any = {}
        for (const attr in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, attr)) {
                copy[attr] = cloneObject(obj[attr])
            }
        }
        return copy
    }
    throw new Error("Unable to copy obj! Its type isn't supported.")
}

//数据对象赋值
export function copyObject(s: any, d: any) {
    for (const attr in d) {
        if (Object.prototype.hasOwnProperty.call(s, attr)) {
            d[attr] = cloneObject(s[attr])
        }
    }
}

/**
 * 校验对象是否为空
 * 注：undefined或null返回true，空字符串""返回true，空对象{}返回true
 * @param {*} data
 * @returns
 */
export function isEmpty(data: any) {
    if (data == undefined || data == null) {
        return true
    }
    if (data.constructor == String || data.constructor == Array) {
        return !(data.length > 0)
    }
    if (data.constructor == Object) {
        for (const key in data) {
            return false
        }
        return true
    }
    return false
}

/**
 * 对象转换Json
 * @param {*} o
 * @returns
 */
export function object2json(o: Object) {
    return JSON.stringify(o)
}

/**
 * 数组转换Json
 * @param {*} o
 * @param {*} name
 * @returns
 */
export function array2json(o: any[], name?: string[]) {
    let result
    if (name) {
        const arr = new Array()
        if (name instanceof Array)
            o.forEach((x) => {
                const n: { [k: string]: any } = new Object()
                name.forEach((y) => {
                    n[y] = x[y]
                })
                arr.push(n)
            })
        else
            o.forEach((x) => {
                arr.push(x[name])
            })
        result = object2json(arr)
    } else {
        result = object2json(o)
    }
    return result
}

/**
 * Json转数组
 * @param {*} o
 * @returns
 */
export function json2array(o: string) {
    try {
        if (o)
            return eval("(" + o + ")")
        else
            return []
    } catch (e) {
        log.error(e)
        return []
    }
}

/**
 * Json转对象
 * @param {*} o
 * @returns
 */
export function json2object(o: any) {
    try {
        if (o)
            return eval("(" + o + ")")
        else
            return {}
    } catch (e) {
        log.error(o)
        return {}
    }
}
